package com.bawei.mvvm

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.LruCache
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.mvvm.adapter.GoodsAdapter
import com.bawei.mvvm.databinding.ActivityMainBinding
import com.bawei.mvvm.entity.Goods
import com.bawei.mvvm.utills.GlideUtils
import com.bawei.mvvm.viewmodel.MainViewModel
import com.bawei.mvvm.widget.MyItemDecoration
import com.facebook.drawee.backends.pipeline.info.ImageLoadStatus
import com.youth.banner.loader.ImageLoader

import dagger.hilt.android.AndroidEntryPoint
import retrofit2.Retrofit
import javax.inject.Inject
@Route(path = "/app/main")
@AndroidEntryPoint
class MainActivity: AppCompatActivity() {

    private val mainViewModel:MainViewModel by viewModels()
    private lateinit var activityMainBinding:ActivityMainBinding
    private lateinit var  goodsAdapter:GoodsAdapter
    private var list:MutableList<Goods> = mutableListOf()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //databing
        activityMainBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(activityMainBinding.root)

        //初始化列表
        goodsAdapter = GoodsAdapter(list)
        activityMainBinding.rv.adapter = goodsAdapter
        activityMainBinding.rv.layoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
        activityMainBinding.rv.addItemDecoration(MyItemDecoration(3,dp2px(10)))
        //轮播图
        mainViewModel.getBanners().observe(this, Observer {
            if(it.code == 200){
                //轮播图展示
                var  mutableList = it.data
                var images = mutableListOf<String>()
                var titles = mutableListOf<String>()
                mutableList.forEach {
                    images.add(it.imagePath)
                    titles.add(it.desc)
                    //将数据存储到数据库中
                    mainViewModel.insertBanner(it)
                    Log.d("ytx", "查询轮播图数据: "+mainViewModel.getBannersFormDB())
                }


                activityMainBinding.banner.let {
                    it.setImages(images)
                    it.setBannerTitles(titles)
                    it.setImageLoader(object :ImageLoader(){
                        override fun displayImage(
                            context: Context?,
                            path: Any?,
                            imageView: ImageView?
                        ) {
                            if(context != null && imageView != null){
                                GlideUtils.loadNormal(context,imageView,path as String)
                            }

                        }

                    })
                    it.start()

                }
            }
        })
        //列表
        mainViewModel.getGoods(0,1,30).observe(this, Observer {
            if(it.code == 200){
                list.addAll(it.data)
                goodsAdapter.notifyDataSetChanged()
            }
        })
    }
    //dp转px
    fun dp2px(defaultDP:Int):Int{
        var scale = App.instance.resources.displayMetrics.density
        return (scale*defaultDP+0.5f).toInt()
    }
}